package com.paipaideli.ui.home.adapter.model;

import android.content.Context;
import android.content.Intent;
import android.support.annotation.NonNull;
import android.support.v7.widget.AppCompatImageView;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.airbnb.epoxy.EpoxyRecyclerView;
import com.jakewharton.rxbinding2.view.RxView;
import com.paipaideli.R;
import com.paipaideli.common.utils.DateUtil;
import com.paipaideli.common.utils.ImageUtil;
import com.paipaideli.common.utils.ToastUtil;
import com.paipaideli.common.views.DownTextView;
import com.paipaideli.ui.home.LotsFragment;
import com.paipaideli.ui.home.bean.HomeBean;
import com.paipaideli.ui.login.LoginActivity;
import com.paipaideli.ui.login.RegisterActivity;
import com.paipaideli.ui.product.ProductDetailActivity;

import java.util.List;
import java.util.concurrent.TimeUnit;

import butterknife.BindView;
import butterknife.ButterKnife;

public class GoodsView extends FrameLayout {

    @BindView(R.id.recycler)
    EpoxyRecyclerView recycler;
    private ViewModelAdapter viewModelAdapter;

    private long start;
    private long end;
    private long now;

    public GoodsView(Context context) {
        super(context);
        init(context);
    }

    List<HomeBean.Data.AuctionInfoRes> values;

    public void setData(List<HomeBean.Data.AuctionInfoRes> values) {
        this.values = values;
        viewModelAdapter.setData(values);
    }

    public void init(Context context) {
        View inflate = View.inflate(context, R.layout.view_goods, this);
        inflate.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT));
        ButterKnife.bind(inflate);

        recycler.setLayoutManager(new LinearLayoutManager(context) {
            @Override
            public boolean canScrollHorizontally() {
                return false;
            }

            @Override
            public boolean canScrollVertically() {
                return false;
            }
        });
        viewModelAdapter = new ViewModelAdapter();
        recycler.setAdapter(viewModelAdapter);

    }

    class ViewModelAdapter extends RecyclerView.Adapter<ViewModelAdapter.ViewHolder> {
        List<HomeBean.Data.AuctionInfoRes> values;

        public void setData(List<HomeBean.Data.AuctionInfoRes> values) {
            this.values = values;
            notifyDataSetChanged();
        }

        @NonNull
        @Override
        public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
            View convertView = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_home_goods, parent, false);
            return new ViewHolder(convertView);
        }

        @Override
        public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
            try {
                now = DateUtil.getCurTimeLong();
                start = values.get(position).startTime;
                end = values.get(position).endTime;
            } catch (Exception e) {
                e.getMessage();
            }
            //将前一个缓存清除
            if (holder.countDown != null) {
                holder.countDown.cancel();
            }
            holder.tv_item_bg.setVisibility(View.GONE);
            if (start - now > 0) {
                holder.countDown.init(start - now, end - start, 0, new DownTextView.CouponDownInterface() {
                    @Override
                    public void runDown() {
                        try {
                            holder.tv_item_bg.setVisibility(View.VISIBLE);
                        }catch (Exception e){

                        }
                    }

                    @Override
                    public void runText(long timer, int type) {

                    }
                });
            } else {
                holder.countDown.init(0, end - now, 1, new DownTextView.CouponDownInterface() {
                    @Override
                    public void runDown() {
                        try {
                            holder.tv_item_bg.setVisibility(View.VISIBLE);
                        }catch (Exception e){

                        }
                    }

                    @Override
                    public void runText(long timer, int type) {

                    }
                });
            }

            holder.item_text_name.setText(values.get(position).goodsName);
            holder.itemQpPrice.setText("￥" + values.get(position).goodsStartingPrice);
            holder.itemScPrice.setText("市场价: " + values.get(position).goodsMarketPrice);
            ImageUtil.displayImage(values.get(position).coverImgPath, holder.item_image);

            //商品点击事件
            RxView.clicks(holder.linCl)
                    .throttleFirst(1, TimeUnit.SECONDS)
                    .subscribe(o -> {
                        if (holder.tv_item_bg.getVisibility() == View.VISIBLE) {
                            ToastUtil.show("本场次已经结束");
                        } else {
                            ProductDetailActivity.open(getContext(), values.get(position).id, "1");
                        }
                    }, throwable -> {
                    });
        }

        @Override
        public int getItemCount() {
            return values.size();
        }

        class ViewHolder extends RecyclerView.ViewHolder {
            @BindView(R.id.item_image)
            AppCompatImageView item_image;
            @BindView(R.id.item_qp_price)
            TextView itemQpPrice;
            @BindView(R.id.item_sc_price)
            TextView itemScPrice;
            @BindView(R.id.countDown)
            DownTextView countDown;
            @BindView(R.id.lin_cl)
            LinearLayout linCl;
            @BindView(R.id.lin_item_good)
            LinearLayout linItemGood;
            @BindView(R.id.item_text_name)
            TextView item_text_name;
            @BindView(R.id.tv_item_bg)
            TextView tv_item_bg;

            public ViewHolder(@NonNull View itemView) {
                super(itemView);
                ButterKnife.bind(this, itemView);
            }
        }
    }


}
